package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.domain.ZzAppDeviceBindCode;
import com.zzedu.dto.ZzAppDeviceBindCodeDTO;
import com.zzedu.mapper.ZzAppDeviceBindCodeMapper;
import com.zzedu.service.IZzAppDeviceBindCodeService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * (教育-学校)设备绑定codeService业务层处理
 *
 * @author Mr.Wang
 * @date 2025-06-02
 */
@Service
public class ZzAppDeviceBindCodeServiceImpl extends ServiceImpl<ZzAppDeviceBindCodeMapper, ZzAppDeviceBindCode> implements IZzAppDeviceBindCodeService {


    @Override
    public LambdaQueryWrapper<ZzAppDeviceBindCode> buildQuery(ZzAppDeviceBindCodeDTO zzAppDeviceBindCode) {
        LambdaQueryWrapper<ZzAppDeviceBindCode> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null != zzAppDeviceBindCode.getSchoolId(), ZzAppDeviceBindCode::getSchoolId, zzAppDeviceBindCode.getSchoolId());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAppDeviceBindCode.getBusDate()), ZzAppDeviceBindCode::getBusDate, zzAppDeviceBindCode.getBusDate());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzAppDeviceBindCode.getBindCode()), ZzAppDeviceBindCode::getBindCode, zzAppDeviceBindCode.getBindCode());
        lambdaQueryWrapper.eq(null != zzAppDeviceBindCode.getExpireAt(), ZzAppDeviceBindCode::getExpireAt, zzAppDeviceBindCode.getExpireAt());
        lambdaQueryWrapper.eq(null != zzAppDeviceBindCode.getGmtCreate(), ZzAppDeviceBindCode::getGmtCreate, zzAppDeviceBindCode.getGmtCreate());
        lambdaQueryWrapper.eq(null != zzAppDeviceBindCode.getGmtModify(), ZzAppDeviceBindCode::getGmtModify, zzAppDeviceBindCode.getGmtModify());
        lambdaQueryWrapper.eq(null != zzAppDeviceBindCode.getDeleted(), ZzAppDeviceBindCode::getDeleted, zzAppDeviceBindCode.getDeleted());

        lambdaQueryWrapper.orderByDesc(ZzAppDeviceBindCode::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public ZzAppDeviceBindCode getCode() {
        ZzAppDeviceBindCode entity = new ZzAppDeviceBindCode();
        int count = 0;
        do {
            try {
                entity.setBindCode(code());
                entity.setExpireAt(LocalDateTime.now().plusMinutes(6L));
                entity.setBusDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
                entity.setSchoolId(SecurityUtils.getSysUser().getUserTypeId());
                this.save(entity);
                return entity;
            } catch (Exception e) {
                try {
                    TimeUnit.MILLISECONDS.sleep(500L);
                } catch (Exception ee) {
                }
                count++;
                continue;
            }
        } while (true && count < 100);
        return null;
    }


    private String code() {
        return RandomStringUtils.secure().nextNumeric(6);
    }
}